import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
// 下载 => 引入 => use => 创建router实例 => 注入Vue实例
// new Router({ routes: [...] })

/* Layout */
// 导入整个首页的架子(layout组件其实没有必要懒加载, 出现频率高)
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

// import Login from '@/views/login/index.vue' => 直接加载的方式
// => 项目一打开的话, 尽管你只看到了登录页, 所有的组件都会加载
// 提供项目的路由规则
// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
// 动态路由表 => 动态路由(需要权限才可以访问的) 我们这里准备一个数组存放
export const asyncRoutes = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter,
  approvalsRouter,
  attendancesRouter,
  salarysRouter,
  socialRouter
]
// 静态路由(不需要权限)
export const constantRoutes = [
  // 登录页的规则 匹配/login, 展示Login组件
  {
    path: '/login',
    // 动态加载(路由懒加载): 只有匹配到了对应的路径, 此时该组件才去加载
    component: () => import('@/views/login/index'),
    hidden: true
  },

  // 404的规则
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // 匹配默认路径, 首页的规则
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      // 首页的二级路由
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  // 导入页的路由规则
  {
    path: '/import',
    component: Layout,
    hidden: true,
    children: [
      { path: '',
        component: () => import('@/views/import/index')
      }
    ]
  }
  // 404 page must be placed at the end !!!
  // 必须放最后 解决04
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => {
  return new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }), // 进行组件切换时, 回到顶部
    // 配置路由规则
    // constantRoutes  [{ path: '', compoennt: ''} , {} ...]
    routes: [
      ...constantRoutes
      // ...asyncRoutes
    ]
  })
}

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由
export function resetRouter() {
  const newRouter = createRouter()
  // 拿新的路由实例的匹配规则 替换之前的路由实例的匹配规则
  router.matcher = newRouter.matcher // reset router
}

// 默认导出路由实例
export default router
